package com.xls.commonlibrary.http;


import com.hjq.gson.factory.GsonFactory;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

// simple get by retrofit
public class GetUtil {

    private static volatile GetUtil singleton = null;
    private Retrofit retrofit;

    private GetUtil() {
        init();
    }

    public static GetUtil getInstance() {
        if (singleton == null) {
            synchronized (GetUtil.class) {
                if (singleton == null) {
                    singleton = new GetUtil();
                }
            }
        }
        return singleton;
    }


    private void init() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        retrofit = new Retrofit.Builder()
                .client(builder.build())
                // todo 动态配置root地址
                .baseUrl("https://api.weixin.qq.com/")
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(GsonFactory.getSingletonGson()))
                .build();
    }

    /**
     * 创建API
     */
    public <T> T create(Class<T> clazz) {
        return retrofit.create(clazz);
    }


}
